import home from './components/home.ejs';
import page404 from './components/404.ejs';
import order from './order/index.js';
import product from './product/index.js';
import deploy from './deploy/index.js';
import user from './user/index.js';
import coupon from './coupon/index.js';
import sms from './sms/index.js';
import finance from './finance/index.js';
import account from './account/index.js';
import distributor from './distributor/index.js';
import supplier from './supplier/index.js';
import permission from './permission/index.js';
import platform from './platform/index.js';
import operating from './tools/operating.js';

import SideMenuTpl from './components/side-menu/html.ejs';

const $wrap = $('.refresh-wrap');
const $breadcrumb = $('.fly-title');

class Router {

	init () {
		let self = this;

		$.ajax({
			url: '/admin/permission/getMenus',
			method: 'post',
			data: JSON.stringify({}),
			contentType: 'application/json',
			dataType: 'json',
			success: function (data) {

				if (data && data.message === 'SUCCESS') {

					$('.fly-side').html(SideMenuTpl(data));

					$('.accordion').accordion({
					    selector: {
					        trigger: '.title .icon'
					    }
					});

					self.bindEvent();
					operating.bindEvent();
				}
			}
        });
	}

	bindEvent () {
		let self = this;

        if (location.hash) {
            self.changeRouter();
        }

		window.addEventListener('hashchange', function () {
			self.changeRouter();
		}, false);

        $('body').on('click', '.router-link', function () {

            let curRouter = $(this).data('router');
			let breadcrumb = $(this).text() || '添加';
            history.pushState({state: curRouter}, '', curRouter);

            self.routerPage(curRouter, breadcrumb);
        });

		$('.fly-home').on('click', function () {
			$wrap.html(home());
		});
	}

	changeRouter () {
		let self = this;
		let curRouter = location.hash;
		let breadcrumb = '';

		$('.router-link').each(function () {

			if ($(this).data('router') === curRouter) {
				breadcrumb = $(this).text();
			}
		});

		self.routerPage(curRouter, breadcrumb);
	}

    routerPage (curRouter, breadcrumb) {
        let path = curRouter.split('_');
		let title = '';

        switch (path[0]) {
           	case '#order':

				title = '订单管理';
           		order.init($wrap);
           		break;
           	case '#product':

				title = '产品管理';
                product.init($wrap, path[1] || null);
           		break;
			case '#platform':

				title = '产品管理';
                platform.init($wrap, path[1] || null);
           		break;
            case '#deploy':

				title = '配置管理';
                deploy.init($wrap, path[1] || null);
           		break;
			case '#account':

				title = '帐号管理';
                account.init($wrap, path[1] || null);
           		break;
			case '#distributor':

				title = '分销商';
                distributor.init($wrap, path[1] || null);
           		break;
			case '#supplier':

				title = '供应商';
                supplier.init($wrap, path[1] || null);
           		break;
			case '#user':

				title = 'C用户数据';
                user.init($wrap, path[1] || null);
           		break;
			case '#coupon':

				title = '代金券管理';
                coupon.init($wrap, path[1] || null);
           		break;
			case '#sms':

				title = '短信发送';
                sms.init($wrap, path[1] || null);
           		break;
			case '#finance':

				title = '财务管理';
                finance.init($wrap);
           		break;
			case '#permission':

				title = '权限管理';
                permission.init($wrap, path[1] || null);
           		break;
           	default:
           		$wrap.html(home());
        }

		$breadcrumb.html($.trim(breadcrumb) != title ? breadcrumb : title);
    }
}

export default new Router();
